package com.yth.utils.udf.function;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yth.utils.udf.Udf;

/**
 * @author yutianhong
 * @version 1.0
 * @since 2024/12/16 14:04
 */
@SuppressWarnings("unused") // used by script
public class JsonUtils implements Udf {

    public static Object parse(String json) {
        return JSONObject.parseObject(json, Feature.OrderedField);
    }

    public static String toString(Object obj) {
        return JSON.toJSONString(obj, SerializerFeature.MapSortField, SerializerFeature.DisableCircularReferenceDetect);
    }

    public static String toStringPretty(Object obj) {
        return JSON.toJSONString(obj, SerializerFeature.MapSortField, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.PrettyFormat);
    }

    public static Object getByJsonPath(Object rootObject, String path) {
        return JSONPath.eval(rootObject, path);
    }

    private JsonUtils() {
    }
}
